package com.lancoo.answer.ui.child.training;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.model.entity.SpokenChildState;
import com.lancoo.answer.ui.ques.SpliteLayoutFragment;
import com.lancoo.answer.widget.combinationView.QueseBodyView;

import java.util.List;

/**
 * Created by pengmi on 2021/12/1 20:53
 * Description:口语作答
 * Add by lijijun on 2022/03/14 11:47 later
 */
public class ChildSpokenAnswerFragment extends Fragment {

    private QueseBodyView tv_queseIndex;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_child_spoken_answer, container, false);
    }

    /**
     * @param kindIndex       List<Type>题型下标
     * @param queseIndex      List<Type>大题下标
     * @param smallQueseIndex List<Type>小题下标
     * @return fragment
     */
    public static Fragment loadChildFragment(int kindIndex, int queseIndex, int smallQueseIndex) {
        ChildSpokenAnswerFragment queseFragment = new ChildSpokenAnswerFragment();
        Bundle mBundle = new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX, queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX, smallQueseIndex);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view) {
        if (getArguments() == null) return;

        int kindIndex = getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int queseIndex = getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int smallQueseIndex = getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        List<Type> kindBeanListk = ConstantBean.Companion.getConstantExamBean().getTypeList();
        Child child = kindBeanListk.get(kindIndex).getQuesList().get(queseIndex).getChildList().get(smallQueseIndex);

        tv_queseIndex = view.findViewById(R.id.quese_body_view);
        boolean enableShowChildAsk = !(getParentFragment() instanceof SpliteLayoutFragment);// 是否显示小题题干
        tv_queseIndex.setEnableShowChildAsk(enableShowChildAsk);
        tv_queseIndex.initData(child);

        if (!enableShowChildAsk) {
            NestedScrollView nsv = (NestedScrollView) view;
            nsv.setFillViewport(true);
        }

        SpokenChildState spokenChildState = ConstantBean.Companion.getTaskControlBean().getSpokenChildState();
        if (spokenChildState == null) return;
        LinearLayout parent = (LinearLayout) tv_queseIndex.getParent();
        View childView = spokenChildState.onCreateView(this, parent, enableShowChildAsk ? 1 : 0, child);
        parent.addView(childView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    }

    /**
     * 关闭音频作答（外部调用）
     */
    public void closeAudioAnswer(){

    }

    public void adjustFontSize(){
        if(tv_queseIndex!=null){
            tv_queseIndex.adjustFontSize();
        }
    }

    public void markQuestion(){
        if(tv_queseIndex!=null){
            tv_queseIndex.markQuestion();
        }
    }
}
